<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .table {
            width: 400px;
            margin: 50px auto;
        }

        .table .tr .item {
            width: 100px;
            height: 40px;
            line-height: 40px;
            color: aquamarine;
            background-color: black;
            text-align: center;
            float: left;
        }

        .success {
            color: green
        }

        .error {
            color: red;
        }
    </style>
</head>

<body>
    <div id="app">
        <div>
            <div>用户名<input @input="setUsername" />
                <span :class="usernameclass">{{usernametip}}</span>
            </div>
            <div>密码<input @input="setUserpwd" />
                <span>{{userpwdtip}}</span>
            </div>
            <div>电话<input @input="setUsertel" />
                <span :class="usertelclass">{{userteltip}}</span>
            </div>
            <button @click="add">提交</button>
        </div>


        <div class="table">
            <div class="tr">
                <div class="item">编号</div>
                <div class="item">名字</div>
                <div class="item">密码</div>
                <div class="item">电话</div>
            </div>

            <div class="tr" v-for="item in userList">
                <div class="item">{{item.id}}</div>
                <div class="item">{{item.username}}</div>
                <div class="item">{{item.userpwd}}</div>
                <div class="item">{{item.usertel}}</div>
            </div>

        </div>
    </div>
</body>
<script>

    new Vue({
        el: "#app",
        data: {
            username: "",
            usernametip: "",
            usernameclass: "",

            userpwd: "",
            userpwdtip: "",

            usertel: "",
            userteltip: "",
            usertelclass: "",

            userList: [{ id: 1, username: "qqq", userpwd: 123456, usertel: 147258 }, { id: 2, username: "aaa", userpwd: 123456, usertel: 1472583 }, { id: 3, username: "bbb", userpwd: 123456, usertel: 14725836 }]
        },

        methods: {
            setUsername(e) {
                this.username = e.target.value;
            },

            setUserpwd(e) {
                this.userpwd = e.target.value;
            },

            setUsertel(e) {
                this.usertel = e.target.value;
            },

            check(value1) {
                var num = 0;
                if (value1.length < 1) return num;
                if (/\d/.test(value1)) { num++ }; //数字
                if (/[a-z]/.test(value1)) { num++ }; //小写
                if (/[A-Z]/.test(value1)) { num++ }; //大写

                switch (num) {
                    case 1:
                        return 1;
                        break;
                    case 2:
                        return 2;
                        break;
                    case 3:
                        return 3;
                        break;
                }
            },

            add() {
                let ids = this.userList.map(item => item.id);
                ids.sort((a, b) => b - a);
                let id = ids[0] ? ids[0] + 1 : 1;
                this.userList.push({ id, username: this.username, userpwd: this.userpwd, usertel: this.usertel });
            }
        },

        watch: {
            username(newvalue) {
                if (this.userList.some(item => item.username == newvalue)) {
                    this.usernametip = "该用户已经存在";
                    this.usernameclass = "error";
                } else {
                    this.usernametip = "√";
                    this.usernameclass = "success";
                }
            },

            userpwd(newname, oldname) {
                let num = this.check(newname);
                switch (num) {
                    case 1:
                        this.userpwdtip = "弱";
                        break;
                    case 2:
                        this.userpwdtip = "中";
                        break;
                    case 3:
                        this.userpwdtip = "强";
                        break;
                }
            },

            usertel(newvalue) {
                if (this.userList.some(item => item.usertel == newvalue)) {
                    this.userteltip = "该手机号已注册，换个试试吧";
                    this.usertelclass = "error";
                } else {
                    this.userteltip = "√";
                    this.usertelclass = "success";
                }
            },
            userList: {
                deep: true,
                handler() {

                }
            }
        }
    })



</script>

</html>